package com.dongao.marketing.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * md5加密工具类
 * @author dongao
 *
 */
public class MD5Util {

	private Logger logger = LoggerFactory.getLogger(MD5Util.class);

	/**
	 * md5加密
	 * 
	 * @param key
	 * @return
	 */
	public static String encrypt(final String key) {
		// md.digest() 该函数返回值为存放哈希值结果的byte数组
		MessageDigest md5;
		StringBuffer hexValue = new StringBuffer();
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] md5Bytes = md5.digest(key.getBytes());
			for (int i = 0; i < md5Bytes.length; i++) {
				int val = ((int) md5Bytes[i]) & 0xff;
				if (val < 16) {
					hexValue.append("0");
				}
				hexValue.append(Integer.toHexString(val));
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		;
		return hexValue.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(MD5Util.encrypt("1初级会计实务教材(2016)5001老师你写的是啥，我看不懂1491021这是啥1480560801876bf65e-2405-476a-8bac-87bedfb5f046"));
	}
}
